﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnxc.Web.xbhinfo
{
    /// <summary>
    /// GetXbh 的摘要说明
    /// </summary>
    public class GetXbh : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.xbhinfo bll = new jnxc.BLL.xbhinfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);            
            else if (ac == "delete")
                delete(context);
            else if (ac == "querymy")
                Querymy(context);
            else if (ac == "ddllist")
                ddllist(context);
        }
        public void ddllist(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string pcc = Web.Admin.myinfo.getPCC(context);
                int level = Web.Admin.myinfo.getLevel(context);
                string  spro, scity, scou;                
                spro = pcc.Split(',')[0];
                scity = pcc.Split(',')[1];
                scou = pcc.Split(',')[2];
                StringBuilder strWhere = new StringBuilder();
                
                    if (spro != "")
                    {
                        strWhere.AppendFormat(" uprovince='{0}' and ", spro);
                    }
                
                    if (scity != "")
                    {
                        strWhere.AppendFormat(" ucity='{0}' and ", scity);
                    }
                
                    if (scou != "")
                    {
                        strWhere.AppendFormat(" ucounty='{0}' and ", scou);
                    }
                    int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                    if (startindex >= 0)
                    {
                        strWhere.Remove(startindex, 3);//删除多余的and关键字  
                    }
                DataSet ds = bll.GetList(strWhere.ToString());
                string strJson = ToJson.DataTable2Json1(ds.Tables[0]);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string uidstr = context.Request.QueryString["xbhid"];
                if (bll.DeleteList(uidstr))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }


        }
       
        public void modify(HttpContext context)
        {
            if (Web.Admin.myinfo.isLogin(context))
            {
                context.Response.ContentType = "text/plain";
                int uid = 0;
                string uidstr = context.Request.Form["xbhid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                string temp = "";
                string jd = context.Request.Form["jd"];
                string wd = context.Request.Form["wd"];
                temp = context.Request.Form["hb"];
                decimal hb = 0;
                if (temp != "") hb = decimal.Parse(temp);
                string xbhbh = context.Request.Form["xbhbh"];
                decimal xbzone = 0;
                temp = context.Request.Form["xbzone"];
                if (temp != "") xbzone = decimal.Parse(temp);
                string lfzc = context.Request.Form["lfzc"];
                string uprovince = context.Request.Form["uprovince"];
                string ucity = context.Request.Form["ucity"];
                string ucounty = context.Request.Form["ucounty"];

                jnxc.Model.xbhinfo model = new jnxc.Model.xbhinfo();
                model.jd = jd;
                model.wd = wd;
                model.hb = hb;
                model.xbhbh = xbhbh;
                model.xbzone = xbzone;
                model.lfzc = lfzc;
                model.uprovince = uprovince;
                model.ucity = ucity;
                model.ucounty = ucounty;
                model.xbhid = uid;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string temp = "";
                string jd = context.Request.Form["jd"];
                string wd = context.Request.Form["wd"];
                temp = context.Request.Form["hb"];
                decimal hb = 0;
                if (temp != "") hb = decimal.Parse(temp);
                string xbhbh = context.Request.Form["xbhbh"];
                decimal xbzone = 0;
                temp = context.Request.Form["xbzone"];
                if (temp != "") xbzone = decimal.Parse(temp);
                string lfzc = context.Request.Form["lfzc"];
                string uprovince = context.Request.Form["uprovince"];
                string ucity = context.Request.Form["ucity"];
                string ucounty = context.Request.Form["ucounty"];

                jnxc.Model.xbhinfo model = new jnxc.Model.xbhinfo();
                model.jd = jd;
                model.wd = wd;
                model.hb = hb;
                model.xbhbh = xbhbh;
                model.xbzone = xbzone;
                model.lfzc = lfzc;
                model.uprovince = uprovince;
                model.ucity = ucity;
                model.ucounty = ucounty;
                int rid = bll.Add(model);
                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                int level = Web.Admin.myinfo.getLevel(context);
                string uname, utruename, st;
                uname = utruename = st = "";
                string  uprovince, ucity,  ucounty;
                 uprovince = ucity  = ucounty = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (null != context.Request.Form["uprovinces"])
                {
                    uprovince = context.Request.Form["uprovinces"].ToString().Trim();
                }
                if (null != context.Request.Form["ucitys"])
                {
                    ucity = context.Request.Form["ucitys"].ToString().Trim();
                }
                if (null != context.Request.Form["ucountys"])
                {
                    ucounty = context.Request.Form["ucountys"].ToString().Trim();
                }        
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" xbhbh like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" lfzc like '%{0}%' and ", utruename);
                }
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (ucounty != "")
                {
                    strWhere.AppendFormat(" ucounty='{0}' and ", ucounty);
                }

                if (ucity != "")
                {
                    strWhere.AppendFormat(" ucity='{0}' and ", ucity);
                }

                if (uprovince != "")
                {
                    strWhere.AppendFormat(" uprovince='{0}' and ", uprovince);

                }               
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string uname, utruename, uprovince, ucity, uunit, ucounty;
                utruename = uname = uprovince = ucity = uunit = ucounty = "";

                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                if (null != context.Request.Form["uname"])
                {
                    uname = context.Request.Form["uname"].ToString().Trim();
                }
                if (null != context.Request.Form["utruename"])
                {
                    utruename = context.Request.Form["utruename"].ToString().Trim();
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }               
                if (null != context.Request.Form["uunit"])
                {
                    uunit = context.Request.Form["uunit"].ToString().Trim();
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" xbhbh like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" lfzc like '%{0}%' and ", utruename);
                }
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (ucounty != "")
                {
                    strWhere.AppendFormat(" ucounty='{0}' and ", ucounty);
                }

                if (ucity != "")
                {
                    strWhere.AppendFormat(" ucity='{0}' and ", ucity);
                }

                if (uprovince != "")
                {
                    strWhere.AppendFormat(" uprovince='{0}' and ", uprovince);

                }               
                
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}